import { inject, observer} from 'mobx-react';
import { WeaSearchGroup,WeaFormItem,WeaLocaleProvider } from 'ecCom';
import { WeaSwitch } from 'comsMobx';
import { Button } from 'antd';
const getLabel = WeaLocaleProvider.getLabel;

@observer
class Condition extends React.Component{

    constructor(){
        super();
        this.formParams = {};
    }

    render(){
        const { condition, form,doSearch,setShowSearchAd } = this.props.listStore;
        const {isFormInit} = form;
        let group = [];
        const formParamsTemp = form.getFormParams();
        for(let param in formParamsTemp){
            this.formParams[param] = formParamsTemp[param];
        }
        isFormInit && condition.map((c,index) =>{
            let items = [];
            c.items.map(fields => {
                items.push({
                    com:(<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@iwfity@${fields.domkey[0]}`}
                        label={`${fields.label}`}
                        labelCol={{span: `${fields.labelcol}`}}
                        wrapperCol={{span: `${fields.fieldcol}`}}>
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@0fycr9@${fields.domkey[0]}`} fieldConfig={fields} form={form} formParams={this.formParams} onEnterSearch={this.onEnterSearch}/>
                        </WeaFormItem>),
                    colSpan:1
                })
            });
            group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@xmxqkm@${index}`} needTigger={true} title={c.title} showGroup={c.defaultshow} items={items}/>)
        });
        return <div>{group}</div>;
    }

    onEnterSearch = () =>{
        const { doSearch,setShowSearchAd } = this.props.listStore;
        doSearch();
        setShowSearchAd(false);
    }
}

const getAdButtons = (listStore, ecId) => {
	const {doSearch,setShowSearchAd,clearFormFields,setConditionByTree} = listStore;
	return [
		(<Button ecId={`${ecId}_Button@li2kiy@search`} type="primary" onClick={()=>{doSearch();setShowSearchAd(false);}}>{getLabel(197,'搜索')}</Button>),
		(<Button ecId={`${ecId}_Button@n9bpie@reset`} type="ghost" onClick={()=>{clearFormFields();setConditionByTree();}}>{getLabel(2022,'重置')}</Button>),
		(<Button ecId={`${ecId}_Button@h2d039@cancel`} type="ghost" onClick={()=>{setShowSearchAd(false)}}>{getLabel(31129,'取消')}</Button>)
	];
}

export {Condition,getAdButtons}
